package com.xjj.service.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjj.entity.Leave;
import com.xjj.entity.ResponseEntity;
import com.xjj.service.service.LeaveService;
import com.xjj.util.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author : 王泰元
 * @date : 2022/10/24 17:17
 */
@RestController
@RequestMapping("/leave/api")
@Slf4j
public class ApiLeaveController {
    @Autowired
    LeaveService leaveService;

    @ApiOperation("请假列表")
    @GetMapping("/getByStudent/{studentId}/{status}")
    public ResponseEntity<List<Leave>> getByStudent(@PathVariable("studentId") Integer studentId,
                                                    @PathVariable(value = "status", required = false) Integer status) {

        if (status == -1) {
            return ResponseEntity.SUCCESS("ok", leaveService.getByStudent(studentId, null));
        }
        return ResponseEntity.SUCCESS("ok", leaveService.getByStudent(studentId, status));
    }


    @ApiOperation("请假申请")
    @PostMapping
    public ResponseEntity<Integer> addLeave(@RequestBody Leave leave) {
        log.debug("/leave/add >>>>>>>>> 入参，leave：{}", leave);

        Integer id = leaveService.addLeave(leave);

        return ResponseEntity.SUCCESS("ok", id);
    }


    @ApiOperation("家长确认")
    @PutMapping("/{id}")
    public ResponseEntity<Boolean> checkLeave(@PathVariable Integer id, HttpServletRequest request) throws JsonProcessingException {
        Integer parentId = JwtUtil.getParent(request.getHeader("token")).getId();
        log.debug("/leave/check >>>>>>>>> 入参，id：{}", id);
        boolean success = leaveService.checkLeave(id, parentId);
        return ResponseEntity.SUCCESS("ok", success);
    }

}
